package blog_test

import (
	"context"
	"testing"

	"gitlab.com/go-course-project/go17/vblog/apps/blog"
)

var (
	ctx = context.Background()
)

// Required("admin")
func TestCreateBlog(t *testing.T) {
	req := &blog.CreateBlogRequest{
		Title:    "Go项目课1",
		Summary:  "全栈项目",
		Content:  "GORM + GIN",
		Category: "软件开发",
		Tags: map[string]string{
			"Language": "Golang",
		},
	}
	ins, err := blog.GetService().CreateBlog(ctx, req)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(ins)
}

func TestUpdateBlog(t *testing.T) {
	ins, err := blog.GetService().DescribeBlog(ctx, blog.NewDescribeBlogRequest(31))
	if err != nil {
		t.Fatal(err)
	}

	req := blog.NewUpdateBlogRequest(31)
	req.CreateBlogRequest = ins.CreateBlogRequest
	req.Summary = "for update"

	ins, err = blog.GetService().UpdateBlog(ctx, req)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(ins)
}

// SELECT * FROM `blogs` ORDER BY created_at DESC LIMIT 20
func TestQueryBlog(t *testing.T) {
	req := blog.NewQueryBlogRequest()
	req.Tags = map[string]string{
		"Language": "Golang",
	}
	ins, err := blog.GetService().QueryBlog(ctx, req)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(ins)
}

func TestDescribeBlog(t *testing.T) {
	req := blog.NewDescribeBlogRequest(31)
	ins, err := blog.GetService().DescribeBlog(ctx, req)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(ins)
}

func TestPublishBlog(t *testing.T) {
	req := blog.NewPublishBlogRequest(31)
	req.Stage = blog.STAGE_PUBLISHED
	ins, err := blog.GetService().PublishBlog(ctx, req)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(ins)
}

func TestNewQueryBlogRequest(t *testing.T) {
	req := blog.NewQueryBlogRequest()
	req.SetTag("key1=value1,key2=value2")
	t.Log(req)
}
